package com.dragon.www.haier.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dragon.www.haier.R;
import com.dragon.www.haier.entity.Room;
import com.dragon.www.haier.util.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;


/**
 * Created by 51268 on 2016/3/10.
 */
public class MainItemView extends RelativeLayout {
    private final String NAMESPACE = "http://schemas.android.com/apk/res-auto";

    private RelativeLayout relativeLayout;
    private ImageView imageIcon;
    private ImageView imageBg;
    private TextView tv_name;
    private TextView tv_status;
    private String desc;
    private String stringImageID;
    private String stringBackgroundID;
    private DisplayImageOptions options;
    private int imageID;

    //有style样式
    public MainItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    //有属性
    public MainItemView(Context context, AttributeSet attrs) {
        super(context, attrs);

        desc = attrs.getAttributeValue(NAMESPACE, "desc");
        stringImageID = attrs.getAttributeValue(NAMESPACE, "myImage");
        stringBackgroundID = attrs.getAttributeValue(NAMESPACE, "myBackground");

        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
                .displayer(new SimpleBitmapDisplayer())
                .build();

        initView();
    }

    //直接new
    public MainItemView(Context context) {
        super(context);
        initView();
    }

    private void initView() {
        View view = View.inflate(getContext(), R.layout.main_view_item, this);
        relativeLayout = (RelativeLayout) view.findViewById(R.id.relative_background);
        imageIcon = (ImageView) view.findViewById(R.id.im_icon);
        imageBg = (ImageView) view.findViewById(R.id.im_bg);
        tv_name = (TextView) view.findViewById(R.id.tv_desc);
//        tv_status = (TextView) view.findViewById(R.id.tv_status);

        if(stringImageID!=null){
            int ImageID=Integer.parseInt(StringUtil.splitResouceID(stringImageID));
            imageIcon.setImageResource(ImageID);
        }
        if (stringBackgroundID != null) {
            int BackgroundID = Integer.parseInt(StringUtil.splitResouceID(stringBackgroundID));
            relativeLayout.setBackgroundResource(BackgroundID);
        }
        tv_name.setText(desc);
    }

    public void setRoomStatus(String desc) {
//        tv_status.setVisibility(View.VISIBLE);
//        tv_status.setText(desc);
    }

    public void setRoomInfo(Room room) {
        if( room != null ){
            tv_name.setText(room.getRoomName());
//            tv_status.setVisibility(View.VISIBLE);
//            tv_status.setText("没有返回值");
            String imagePath = room.getImgPath();
            if (!TextUtils.isEmpty(imagePath)) {
                imageBg.setVisibility(VISIBLE);
                imageIcon.setVisibility(GONE);
                ImageLoader.getInstance().displayImage(imagePath, imageBg, options);
                imageBg.setScaleType(ImageView.ScaleType.FIT_XY);
            }else{
                imageBg.setVisibility(GONE);
                imageIcon.setVisibility(VISIBLE);
            }
        }else{
            tv_name.setText("房间 ");
//            tv_status.setVisibility(View.VISIBLE);
//            tv_status.setText("没有房间连接");
            imageBg.setVisibility(GONE);
            imageIcon.setVisibility(VISIBLE);
        }
    }

    public void setRoomLoginOut() {
//        tv_status.setVisibility(View.GONE);
        tv_name.setText("房间");
        imageBg.setVisibility(GONE);
        imageIcon.setVisibility(VISIBLE);
    }
}
